home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Sound / RAPlay / src / decode144.c < prev    next >
C/C++ Source or Header  |  2000-12-19  |  10KB  |  438 lines

  1. #include "common.h"
  2. #include "decode144.h"
  3. #include "tables144.h"
  4.  
  5. /* Initialize internal variable structure */
  6. Real_144 *init_144(void)
  7. {
  8.     Real_internal *glob;
  9.  
  10.     if ((glob=malloc(sizeof(Real_internal))))
  11.       memset(glob,0,sizeof(Real_internal));
  12.  
  13.     glob->resetflag=1;
  14.     glob->swapbuf1=glob->swapb1a;
  15.     glob->swapbuf2=glob->swapb2a;
  16.     glob->swapbuf1alt=glob->swapb1b;
  17.     glob->swapbuf2alt=glob->swapb2b;
  18.  
  19.     memcpy(glob->wavtable1,wavtable1,sizeof(wavtable1));
  20.     memcpy(glob->wavtable2,wavtable2,sizeof(wavtable2));
  21.  
  22.     return (Real_144 *)glob;
  23. }
  24.  
  25. /* Free internal variable structure */
  26. void free_144(Real_144 *global)
  27. {
  28.     if (!global) return;
  29.     free(global);
  30. }
  31.  
  32. /* Uncompress one block (20 bytes -> 160*2 bytes) */
  33. void decode_144(Real_144 *global, unsigned char *source, signed short *target)
  34. {
  35.   unsigned int a,b,c;
  36.   long s;
  37.   signed short *shptr;
  38.   unsigned int *lptr,*temp;
  39.   const short **dptr;
  40.   Real_internal *glob;
  41.  
  42.   if (!global) return;
  43.   glob = (Real_internal *)global;
  44.  
  45.   unpack_input(source,glob->unpacked);
  46.   
  47.   glob->iptr=glob->unpacked;
  48.   glob->val=decodetable[0][(*(glob->iptr++))<<1];
  49.  
  50.   dptr=decodetable+1;
  51.   lptr=glob->swapbuf1;
  52.   while (lptr<glob->swapbuf1+10)
  53.     *(lptr++)=(*(dptr++))[(*(glob->iptr++))<<1];
  54.  
  55.   do_voice(glob->swapbuf1,glob->swapbuf2);
  56.  
  57.   a=t_sqrt(glob->val*glob->oldval)>>12;
  58.  
  59.   for (c=0;c<NBLOCKS;c++) {
  60.     if (c==(NBLOCKS-1)) {
  61.       dec1(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val);
  62.     } else {
  63.       if (c*2==(NBLOCKS-2)) {
  64.         if (glob->oldval<glob->val) {
  65.           dec2(glob,glob->swapbuf1,glob->swapbuf2,3,a,glob->swapbuf2alt,c);
  66.         } else {
  67.           dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,a,glob->swapbuf2,c);
  68.         }
  69.       } else {
  70.         if (c*2<(NBLOCKS-2)) {
  71.           dec2(glob,glob->swapbuf1alt,glob->swapbuf2alt,3,glob->oldval,glob->swapbuf2,c);
  72.         } else {
  73.           dec2(glob,glob->swapbuf1,glob->swapbuf2,3,glob->val,glob->swapbuf2alt,c);
  74.         }
  75.       }
  76.     }
  77.   }
  78.  
  79.   /* do output */
  80.   for (b=0,c=0;c<4;c++) {
  81.     glob->gval=glob->gbuf1[c*2];
  82.     glob->gsp=glob->gbuf2+b;
  83.     do_output_subblock(glob,glob->resetflag);
  84.     glob->resetflag=0;
  85.  
  86.     shptr=glob->output_buffer;
  87.     while (shptr<glob->output_buffer+BLOCKSIZE) {
  88.       s=*(shptr++)<<2;
  89.       *target=s;
  90.       if (s>32767) *target=32767;
  91.       if (s<-32767) *target=-32768;
  92.       target++;
  93.     }
  94.     b+=30;
  95.   }
  96.  
  97.   glob->oldval=glob->val;
  98.   temp=glob->swapbuf1alt;
  99.   glob->swapbuf1alt=glob->swapbuf1;
  100.   glob->swapbuf1=temp;
  101.   temp=glob->swapbuf2alt;
  102.   glob->swapbuf2alt=glob->swapbuf2;
  103.   glob->swapbuf2=temp;
  104. }
  105.  
  106. /* lookup square roots in table */
  107. static int t_sqrt(unsigned int x)
  108. {
  109.   int s=0;
  110.   while (x>0xfff) { s++; x=x>>2; }
  111.   return (sqrt_table[x]<<s)<<2;
  112. }
  113.  
  114. /* do 'voice' */
  115. static void do_voice(int *a1, int *a2)
  116. {
  117.   int buffer[10];
  118.   int *b1,*b2;
  119.   int x,y;
  120.   int *ptr,*tmp;
  121.   
  122.   b1=buffer;
  123.   b2=a2;
  124.   
  125.   for (x=0;x<10;x++) {
  126.     b1[x]=(*a1)<<4;
  127.  
  128.     if(x>0) {
  129.       ptr=b2+x;
  130.       for (y=0;y<=x-1;y++)
  131.         b1[y]=(((*a1)*(*(--ptr)))>>12)+b2[y];
  132.     }
  133.     tmp=b1;
  134.     b1=b2;
  135.     b2=tmp;
  136.     a1++;
  137.   }  
  138.   ptr=a2+10;
  139.   while (ptr>a2) (*a2++)>>=4;
  140. }
  141.  
  142.  
  143. /* do quarter-block output */
  144. static void do_output_subblock(Real_internal *glob, int x)
  145. {
  146.   int a,b,c,d,e,f,g;
  147.  
  148.   if (x==1) memset(glob->buffer,0,20);
  149.   if ((*glob->iptr)==0) a=0;
  150.   else a=(*glob->iptr)+HALFBLOCK-1;
  151.   glob->iptr++;
  152.   b=*(glob->iptr++);
  153.   c=*(glob->iptr++);
  154.   d=*(glob->iptr++);
  155.   if (a) rotate_block(glob->buffer_2,glob->buffer_a,a);
  156.   memcpy(glob->buffer_b,etable1+b*BLOCKSIZE,BLOCKSIZE*2);
  157.   e=((ftable1[b]>>4)*glob->gval)>>8;
  158.   memcpy(glob->buffer_c,etable2+c*BLOCKSIZE,BLOCKSIZE*2);
  159.   f=((ftable2[c]>>4)*glob->gval)>>8;
  160.   if (a) g=irms(glob->buffer_a,glob->gval)>>12;
  161.   else g=0;
  162.   add_wav(glob,d,a,g,e,f,glob->buffer_a,glob->buffer_b,glob->buffer_c,glob->buffer_d);
  163.   memmove(glob->buffer_2,glob->buffer_2+BLOCKSIZE,(BUFFERSIZE-BLOCKSIZE)*2);
  164.   memcpy(glob->buffer_2+BUFFERSIZE-BLOCKSIZE,glob->buffer_d,BLOCKSIZE*2);
  165.   final(glob,glob->gsp,glob->buffer_d,glob->output_buffer,glob->buffer,BLOCKSIZE);
  166. }
  167.  
  168. /* rotate block */
  169. static void rotate_block(short *source, short *target, int offset)
  170. {
  171.   short *end;
  172.   short *ptr1;
  173.   short *ptr2;
  174.   short *ptr3;
  175.   ptr2=source+BUFFERSIZE;
  176.   ptr3=ptr1=ptr2-offset;
  177.   end=target+BLOCKSIZE;
  178.   while (target<end) {
  179.     *(target++)=*(ptr3++);
  180.     if (ptr3==ptr2) ptr3=ptr1;
  181.   }
  182. }
  183.  
  184. /* inverse root mean square */
  185. static int irms(short *data, int factor)
  186. {
  187.   short *p1,*p2;
  188.   unsigned int sum;
  189.   p2=(p1=data)+BLOCKSIZE;
  190.   for (sum=0;p2>p1;p1++) sum+=(*p1)*(*p1);
  191.   if (sum==0) return 0; /* OOPS - division by zero */
  192.   return (0x20000000/(t_sqrt(sum)>>8))*factor;
  193. }
  194.  
  195. /* multiply/add wavetable */
  196. static void add_wav(Real_internal *glob, int n, int f, int m1, int m2, int m3, short *s1, short *s2, short *s3, short *dest)
  197. {
  198.   int a,b,c;
  199.   int x;
  200.   short *ptr,*ptr2;
  201.  
  202.   ptr=glob->wavtable1+n*9;
  203.   ptr2=glob->wavtable2+n*9;
  204.   if (f!=0) {
  205.     a=((*ptr)*m1)>>((*ptr2)+1); 
  206.   } else {
  207.     a=0;
  208.   }
  209.   ptr++;ptr2++;
  210.   b=((*ptr)*m2)>>((*ptr2)+1);
  211.   ptr++;ptr2++;
  212.   c=((*ptr)*m3)>>((*ptr2)+1);
  213.   ptr2=(ptr=dest)+BLOCKSIZE;
  214.   if (f!=0)
  215.     while (ptr<ptr2)
  216.       *(ptr++)=((*(s1++))*a+(*(s2++))*b+(*(s3++))*c)>>12;
  217.   else
  218.     while (ptr<ptr2)
  219.       *(ptr++)=((*(s2++))*b+(*(s3++))*c)>>12;
  220. }
  221.  
  222.  
  223. static void final(Real_internal *glob, short *i1, short *i2, void *out, int *statbuf, int len)
  224. {
  225.   int x,sum;
  226.   int buffer[10];
  227.   short *ptr;
  228.   short *ptr2;
  229.  
  230.   memcpy(glob->work,statbuf,20);
  231.   memcpy(glob->work+10,i2,len*2);
  232.  
  233.   buffer[9]=i1[0];
  234.   buffer[8]=i1[1];
  235.   buffer[7]=i1[2];
  236.   buffer[6]=i1[3];
  237.   buffer[5]=i1[4];
  238.   buffer[4]=i1[5];
  239.   buffer[3]=i1[6];
  240.   buffer[2]=i1[7];
  241.   buffer[1]=i1[8];
  242.   buffer[0]=i1[9];
  243.  
  244.   ptr2=(ptr=glob->work)+len;
  245.   while (ptr<ptr2) {
  246.     for(sum=0,x=0;x<=9;x++)
  247.       sum+=buffer[x]*(ptr[x]);
  248.     sum=sum>>12;
  249.     x=ptr[10]-sum;
  250.     if (x<-32768 || x>32767)
  251.     {
  252.       memset(out,0,len*2);
  253.       memset(statbuf,0,20);
  254.       return;
  255.     }
  256.     ptr[10]=x;
  257.     ptr++;
  258.   }
  259.   memcpy(out,ptr+10-len,len*2);
  260.   memcpy(statbuf,ptr,20);
  261. }
  262.  
  263. /* Decode 20-byte input */
  264. static void unpack_input(unsigned char *input, unsigned int *output)
  265. {
  266.   unsigned int outbuffer[28];
  267.   unsigned short inbuffer[10];
  268.   unsigned int x;
  269.   unsigned int *ptr;
  270.  
  271.   /* fix endianness */
  272.   for (x=0;x<20;x+=2)
  273.     inbuffer[x/2]=(input[x]<<8)+input[x+1];
  274.  
  275.   /* unpack */
  276.   ptr=outbuffer;
  277.   *(ptr++)=27;
  278.   *(ptr++)=(inbuffer[0]>>10)&0x3f;
  279.   *(ptr++)=(inbuffer[0]>>5)&0x1f;
  280.   *(ptr++)=inbuffer[0]&0x1f;
  281.   *(ptr++)=(inbuffer[1]>>12)&0xf;
  282.   *(ptr++)=(inbuffer[1]>>8)&0xf;
  283.   *(ptr++)=(inbuffer[1]>>5)&7;
  284.   *(ptr++)=(inbuffer[1]>>2)&7;
  285.   *(ptr++)=((inbuffer[1]<<1)&6)|((inbuffer[2]>>15)&1);
  286.   *(ptr++)=(inbuffer[2]>>12)&7;
  287.   *(ptr++)=(inbuffer[2]>>10)&3;
  288.   *(ptr++)=(inbuffer[2]>>5)&0x1f;
  289.   *(ptr++)=((inbuffer[2]<<2)&0x7c)|((inbuffer[3]>>14)&3);
  290.   *(ptr++)=(inbuffer[3]>>6)&0xff;
  291.   *(ptr++)=((inbuffer[3]<<1)&0x7e)|((inbuffer[4]>>15)&1);
  292.   *(ptr++)=(inbuffer[4]>>8)&0x7f;
  293.   *(ptr++)=(inbuffer[4]>>1)&0x7f;
  294.   *(ptr++)=((inbuffer[4]<<7)&0x80)|((inbuffer[5]>>9)&0x7f);
  295.   *(ptr++)=(inbuffer[5]>>2)&0x7f;
  296.   *(ptr++)=((inbuffer[5]<<5)&0x60)|((inbuffer[6]>>11)&0x1f);
  297.   *(ptr++)=(inbuffer[6]>>4)&0x7f;
  298.   *(ptr++)=((inbuffer[6]<<4)&0xf0)|((inbuffer[7]>>12)&0xf);
  299.   *(ptr++)=(inbuffer[7]>>5)&0x7f;
  300.   *(ptr++)=((inbuffer[7]<<2)&0x7c)|((inbuffer[8]>>14)&3);
  301.   *(ptr++)=(inbuffer[8]>>7)&0x7f;
  302.   *(ptr++)=((inbuffer[8]<<1)&0xfe)|((inbuffer[9]>>15)&1);
  303.   *(ptr++)=(inbuffer[9]>>8)&0x7f;
  304.   *(ptr++)=(inbuffer[9]>>1)&0x7f;
  305.  
  306.   *(output++)=outbuffer[11];
  307.   for (x=1;x<11;*(output++)=outbuffer[x++]);
  308.   ptr=outbuffer+12;
  309.   for (x=0;x<16;x+=4)
  310.   {
  311.     *(output++)=ptr[x];
  312.     *(output++)=ptr[x+2];
  313.     *(output++)=ptr[x+3];
  314.     *(output++)=ptr[x+1];    
  315.   }
  316. }
  317.  
  318. static unsigned int rms(int *data, int f)
  319. {
  320.   int *c;
  321.   int x;
  322.   int d;
  323.   unsigned int res;
  324.   int b;
  325.  
  326.   c=data;
  327.   b=0;
  328.   res=0x10000;
  329.   for (x=0;x<10;x++)
  330.   {
  331.     res=(((0x1000000-(*c)*(*c))>>12)*res)>>12;
  332.     if (res==0) return 0;
  333.     if (res<=0x3fff)
  334.     {
  335.       while (res<=0x3fff)
  336.       {
  337.         b++;
  338.         res<<=2;
  339.       }
  340.     } else {
  341.       if (res>0x10000)
  342.         return 0; /* We're screwed, might as well go out with a bang. :P */
  343.     }
  344.     c++;
  345.   }
  346.   if (res>0) res=t_sqrt(res);
  347.  
  348.   res>>=(b+10);
  349.   res=(res*f)>>10;
  350.   return res;
  351. }
  352.  
  353. static void dec1(Real_internal *glob, int *data, int *inp, int n, int f)
  354. {
  355.   short *ptr,*end;
  356.  
  357.   *(glob->decptr++)=rms(data,f);
  358.   glob->decptr++;
  359.   end=(ptr=glob->decsp)+(n*10);
  360.   while (ptr<end) *(ptr++)=*(inp++);
  361. }
  362.  
  363. static void dec2(Real_internal *glob, int *data, int *inp, int n, int f, int *inp2, int l)
  364. {
  365.   unsigned int *ptr1,*ptr2;
  366.   int work[10];
  367.   int a,b;
  368.   int x;
  369.   int result;
  370.  
  371.   if(l+1<NBLOCKS/2) a=NBLOCKS-(l+1);
  372.   else a=l+1;
  373.   b=NBLOCKS-a;
  374.   if (l==0)
  375.   {
  376.     glob->decsp=glob->sptr=glob->gbuf2;
  377.     glob->decptr=glob->gbuf1;
  378.   }
  379.   ptr1=inp;
  380.   ptr2=inp2;
  381.   for (x=0;x<10*n;x++)
  382.     *(glob->sptr++)=(a*(*ptr1++)+b*(*ptr2++))>>2;
  383.   result=eq(glob,glob->decsp,work);
  384.   if (result==1)
  385.   {
  386.     dec1(glob,data,inp,n,f);
  387.   } else {
  388.     *(glob->decptr++)=rms(work,f);
  389.     glob->decptr++;
  390.   }
  391.   glob->decsp+=n*10;
  392. }
  393.  
  394.  
  395. static int eq(Real_internal *glob, short *in, int *target)
  396. {
  397.   int retval;
  398.   int a;
  399.   int b;
  400.   int c;
  401.   unsigned int u;
  402.   short *sptr;
  403.   int *ptr1,*ptr2,*ptr3;
  404.   int *bp1,*bp2,*temp;
  405.  
  406.   retval=0;
  407.   bp1=glob->buffer1;
  408.   bp2=glob->buffer2;
  409.   ptr2=(ptr3=glob->buffer2)+9;
  410.   sptr=in;
  411.   while (ptr2>=ptr3)
  412.     *(ptr3++)=*(sptr++);
  413.  
  414.   target+=9;
  415.   a=bp2[9];
  416.   *target=a;
  417.   if (a+0x1000>0x1fff)
  418.     return 0; /* We're screwed, might as well go out with a bang. :P */
  419.   c=8;u=a;
  420.   while (c>=0)
  421.   {
  422.     if (u==0x1000) u++;
  423.     if (u==0xfffff000) u--;
  424.     b=0x1000-((u*u)>>12);
  425.     if (b==0) b++;
  426.     ptr2=bp1;
  427.     ptr1=(ptr3=bp2)+c;
  428.     for (u=0;u<=c;u++)
  429.       *(ptr2++)=((*(ptr3++)-(((*target)*(*(ptr1--)))>>12))*(0x1000000/b))>>12;
  430.     *(--target)=u=bp1[(c--)];
  431.     if ((u+0x1000)>0x1fff) retval=1;
  432.     temp=bp2;
  433.     bp2=bp1;
  434.     bp1=temp;
  435.   }
  436.   return retval;
  437. }
  438.